import requests
from django.shortcuts import render, HttpResponse
from rest_framework.viewsets import ModelViewSet
from db_access import models
from db_access.serializers.cmdb_serializer import *
from django.http import JsonResponse
import json


# Create your views here.


class CMDBAssets(ModelViewSet):
    queryset = models.Assets.objects.all()
    serializer_class = AssetsSerializer

    def get_server(self, request):

        pass


# 增删改查
class CMDBServer(ModelViewSet):
    queryset = models.Server.objects.all()
    serializer_class = ServerSerializer


# 增删改查
class CMDBDisk(ModelViewSet):
    queryset = models.Disk.objects.all()
    serializer_class = DiskSerializer


# 增删改查
class CMDBMemory(ModelViewSet):
    queryset = models.Memory.objects.all()
    serializer_class = MemorySerializer


# 增删改查
class CMDBNic(ModelViewSet):
    queryset = models.Nic.objects.all()
    serializer_class = NicSerializer


# 增删改查
class CMDBAssetsChange(ModelViewSet):
    queryset = models.AssetsChange.objects.all()
    serializer_class = AssertChangeSerializer

    def get_server_id(self, request, *args, **kwargs):
        server_id = request.GET.get('server_id')
        if server_id:
            server_obj = models.Server.objects.filter(id=server_id).first()
            if server_obj:
                assets_list = server_obj.assetschange_set.all().values()
                return JsonResponse(list(assets_list), safe=False)
            else:
                return JsonResponse({'msg': 'server_id错误！'})
        else:
            return self.list(request, *args, **kwargs)


# 增删改查
class CMDBErrorLog(ModelViewSet):
    queryset = models.ErrorLog.objects.all()
    serializer_class = ErrorLogSerializer
